﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Player;

namespace UI
{
    public class UIShortcutMenu : MonoBehaviour
    {
        private void Awake()
        {
            foreach (var btn in GetComponentsInChildren<Button>())
            {
                var tmp = btn;
                btn.onClick.AddListener(() => OnBtnClick(tmp));

                if (btn.name == "ScienceButton")
                {
                    Text text = btn.GetComponentInChildren<Text>();
                    Player.MyPlayer.Ins.MyScience.EventOnNumChange += () => text.text = $"Science\n{Player.MyPlayer.Ins.MyScience.ScienceNum}";
                }
                else if (btn.name == "ShopButton")
                {
                    Text text = btn.GetComponentInChildren<Text>();
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    Player.MyPlayer.Ins.MyBackpack.EventOnGoldChange += () =>
                    {
                        sb.Clear();
                        sb.Append("Shop\n￥");
                        sb.Append(Player.MyPlayer.Ins.MyBackpack.Gold);
                        sb.Append("\n");
                        sb.Append(Player.MyPlayer.Ins.MyShop.RefreshCD);
                        text.text = sb.ToString();
                    };
                }
                else if (btn.name == "BombButton")
                {
                    Text text = btn.GetComponentInChildren<Text>();

                    Player.MyPlayer.Ins.MyBackpack.EventOnBombCountChange += () => text.text = $"Bomb\n{MyPlayer.Ins.MyBackpack.BombCount}";
                }
            }
        }
        
        private void OnBtnClick(Button btn)
        {
            switch (btn.name)
            {
                case "ScienceButton":
                    UIScience.Ins.IsOpen = !UIScience.Ins.IsOpen;
                    break;
                case "ShopButton":
                    UIShop.Ins.IsOpen = !UIShop.Ins.IsOpen;
                    break;
                case "BombButton":
                    if (MyPlayer.Ins.MyBackpack.BombCount > 0)
                        MyPlayer.Ins.UseBomb();
                    break;
                default:
                    break;
            }
        }
    }
}